all files / table/ tbody.vue

2.74% Statements 2/73
0% Branches 0/49
0% Functions 0/24
1.85% Lines 1/54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                                                                                                                                                                         
//
//
//
//
//
//
 
export default {
  name: 'mu-tbody',
  data () {
    return {
      selectedRows: []
    }
  },
  created () {
    this.isTbody = true
    this._unSelectAll = false
  },
  computed: {
    showCheckbox () {
      return this.$parent.showCheckbox
    },
    selectable () {
      return this.$parent.selectable
    },
    multiSelectable () {
      return this.$parent.multiSelectable
    },
    enableSelectAll () {
      return this.$parent.enableSelectAll
    },
    isSelectAll () {
      return this.$parent.isSelectAll
    }
  },
  methods: {
    handleRowClick (e, tr) {
      this.$parent.handleRowClick(this.getRowIndex(tr), tr.rowId, tr)
    },
    selectRow (rowId) {
      if (!this.selectable) return
      const index = this.selectedRows.indexOf(rowId)
      if (index === -1) {
        if (!this.multiSelectable) this.selectedRows = []
        this.selectedRows.push(rowId)
        if (this.isSelectAllRow()) {
          this.selectAll(true)
          return
        }
        if (this.$parent.handleRowSelect) this.$parent.handleRowSelect(this.convertSelectedRows(this.selectedRows))
      }
    },
    isSelectAllRow () {
      let count = 0
      this.$children.forEach((child) => {
        if (child.selectable) count++
      })
      return count === this.selectedRows.length
    },
    unSelectRow (rowId) {
      if (!this.selectable) return
      const index = this.selectedRows.indexOf(rowId)
      if (index !== -1) this.selectedRows.splice(index, 1)
      this._unSelectAll = true
      this.$parent.changeSelectAll(false)
      if (this.$parent.handleRowSelect) this.$parent.handleRowSelect(this.convertSelectedRows(this.selectedRows))
    },
    selectAll (isSelectAll) {
      if (!this.selectable || !this.multiSelectable) return
      this._unSelectAll = false
      if (!isSelectAll) {
        this.selectedRows = []
        this.$children.forEach((child) => {
          if (child.selectable) this.selectedRows.push(child.rowId)
        })
      }
      this.$parent.changeSelectAll(true)
      if (this.$parent.handleRowSelect) this.$parent.handleRowSelect(this.convertSelectedRows(this.selectedRows))
    },
    unSelectAll () {
      if (!this.selectable || !this.multiSelectable || this._unSelectAll) return
      this.selectedRows = []
      this.$parent.changeSelectAll(false)
    },
    handleCellClick (event, columnName, td, rowId, tr) {
      if (this.$parent.handleCellClick) this.$parent.handleCellClick(this.getRowIndex(tr), columnName, td, rowId, tr)
    },
    handleCellHover (event, columnName, td, rowId, tr) {
      if (this.$parent.handleCellHover) this.$parent.handleCellHover(this.getRowIndex(tr), columnName, td, rowId, tr)
    },
    handleCellHoverExit (event, columnName, td, rowId, tr) {
      if (this.$parent.handleCellHoverExit) this.$parent.handleCellHoverExit(this.getRowIndex(tr), columnName, td, rowId, tr)
    },
    handleRowHover (event, rowId, tr) {
      if (this.$parent.handleRowHover) this.$parent.handleRowHover(this.getRowIndex(tr), rowId, tr)
    },
    handleRowHoverExit (event, rowId, tr) {
      if (this.$parent.handleRowHoverExit) this.$parent.handleRowHoverExit(this.getRowIndex(tr), rowId, tr)
    },
    getRowIndex (tr) {
      return this.$children.indexOf(tr)
    },
    convertSelectedRows () {
      const selectedIndexs = this.selectedRows.map(rowId => this.convertRowIdToIndex(rowId))
      return this.multiSelectable ? selectedIndexs : selectedIndexs[0]
    },
    convertRowIdToIndex (rowId) {
      for (var i = 0; i < this.$children.length; i++) {
        const child = this.$children[i]
        if (child.rowId && child.rowId === rowId) return i
      }
      return -1
    }
  }
}